﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Chat.GUI
{
    /// <summary>
    /// The base dialog with sceleton functionality for OK & Cancel pressed processing.
    /// </summary>
    public partial class BaseDialog : Form
    {
        public BaseDialog()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
                return;

            UpdateModel();

            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (DataModified)
                RestoreFromModel();

            baseErrorProvider.Clear();

            DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// Abstract. Updates the data model of the dialog with values, that
        /// are currently on the dialog controls.
        /// </summary>
        protected virtual void UpdateModel()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Abstract. Validates the values that are currently on the dialog controls.
        /// </summary>
        /// <returns>true if the data on controls valid, false - otherwise.</returns>
        protected virtual bool ValidateData()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Gets the value, indicating whether the values that are currently on the 
        /// dialog control differ from the values that the dialog data model
        /// currently has.
        /// </summary>
        protected virtual bool DataModified
        {
            get { return true; }
        }
        /// <summary>
        /// Abstract. Restores the values of the dialog controls with the values that
        /// are currently in the dialog data model.
        /// </summary>
        /// <returns>
        /// true, if the values of the dialog controls were successfully restored
        /// from the model; false - otherwise (the model might be not initialized, or null).
        /// </returns>
        protected virtual bool RestoreFromModel()
        {
            throw new NotImplementedException();
        }

        protected void ShowErrorMsg(Control c, string errMsg)
        {
            baseErrorProvider.SetError(c, errMsg);
        }

        protected void ClearError(Control c)
        {
            baseErrorProvider.SetError(c, string.Empty);
        }

    }
}
